package com.cloudclassroom.controller;

import com.cloudclassroom.entity.Customer;
import com.cloudclassroom.mapper.CustomerMapper;
import com.cloudclassroom.vo.RestResult;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author 92110
 */
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class CustomerController {
    private final PasswordEncoder encoder;
    private final CustomerMapper customerMapper;

    @PostMapping("/register")
    public RestResult<String> insertUser(@RequestBody Customer customer) {
        String uuid = UUID.randomUUID().toString().toUpperCase();
        customer.setCustomerId(uuid);
        customer.setPassword(encoder.encode(customer.getPassword()));
        try {
            customerMapper.insert(customer);
            return RestResult.success("用户注册成功！");
        } catch (Exception e) {
            return RestResult.failure(500, e.getMessage());
        }
    }
}
